package com.bysj.servelt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.bysj.model.Evaluation;
import com.bysj.model.OrderEvaluation;
import com.bysj.model.Reply;
import com.bysj.model.User;
import com.bysj.service.CommentService;
import com.bysj.service.EvaluationService;
import com.bysj.service.OrderService;

/**
 * Servlet implementation class AdminOrderReplyAddServlet
 */
@WebServlet("/windowshoper/order_reply_add")
public class WindowshoperOrderReplyAddServlet extends HttpServlet {
	private OrderService oService = new OrderService();
	private CommentService cService = new CommentService();
	private EvaluationService eService = new EvaluationService();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		if (user != null) {
//			int orderId = Integer.parseInt(request.getParameter("orderId"));
//			int commId = Integer.parseInt(request.getParameter("commId"));
//			String content = request.getParameter("content");
//			Reply vo = new Reply();
//			vo.setContent(content);
//			vo.setOrderId(orderId);
//			vo.setCommId(commId);
//			vo.setUserId(user.getId());
//			vo.setUsername(user.getName());

			BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
			StringBuilder responseStrBuilder = new StringBuilder();
			String inputStr;

			while ((inputStr = streamReader.readLine()) != null)
			    responseStrBuilder.append(inputStr);
	        Map<String ,Object> map = JSONObject.parseObject(responseStrBuilder.toString(),Map.class);
	        int orderId = Integer.valueOf(String.valueOf(map.get("orderId")));
			ArrayList<Evaluation> obj=JSON.parseObject(map.get("evaluations").toString(), new TypeReference<ArrayList<Evaluation>>() { });
			List<Evaluation>  evaluations = obj;
			OrderEvaluation o = new OrderEvaluation();
			o.setEvaluations(evaluations);
			eService.addReply(o,user);
			oService.updateStatus(orderId, 6);
			request.getRequestDispatcher("/windowshoper/order_list").forward(request, response);
		} else {
			request.setAttribute("msg", "登录过期，请重新登录！");
			request.getRequestDispatcher("/windowshoper/reply.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

}
